﻿/*****************************************************************************
* File			: src.EnemyMissile
* Purpose		: Contains Declaration and Implementation for EnemyMissile class
* Creation date : April 24, 2009
* Author		: Sami TOUIL
******************************************************************************/
package src 
{
	// Imports
	import flash.display.MovieClip;
	import flash.events.*
	
	/**************************************
	* Class		: EnemyMissile
	* Purpose	: Logic for enemies missiles behavior
	* Parent	: MovieClip
	**************************************/
	public class EnemyMissile extends MovieClip
	{
		// Class variables
		private var nXSpeed			:int;
		private var nYSpeed			:int;
		private var nSpeed			:int;
		private var nRotation		:int;
	
		// Constructor
		public function EnemyMissile(nX, nY, nRotationParam, nXSpeedParam, nYSpeedParam)
		{
			// Set listener for ENTER_FRAME event
			addEventListener(Event.ENTER_FRAME, enterFrame);
			
			// Set coords
			x = nX;
			y = nY;
			
			// Set rotation
			nRotation = nRotationParam;
			
			// Set speed
			nXSpeed = nXSpeedParam;
			nYSpeed = nYSpeedParam;
			
			// Add Missile sprite
			Game.main.spriteClip.addChild(this);
		}
		
		/***********************************************************
		* Method		: enterFrame
		* Purpose		: frame loop
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function enterFrame(e:Event)
		{
			// Clear if the hero ship is dead
			if (Game.main.ship == null || Game.main.ship.bFinalMove)
			{
				onDelete();
				return;
			}
			else
			{
				// Missile movement
				x -= nXSpeed;
				y += nYSpeed;
				
				// Missile rotation
				rotation += nRotation;
				
				// Collision management with Ship
				if(this.hitTestObject(Game.main.ship))
				{
					// Shake ship
					Game.main.ship.initShake();
					
					// Remove health from ship
					Game.main.ship.updateHealth(-10);
					
					// Delete missile instance
					onDelete();
					return;
				}
				// Collision management with Modules
				if(Game.main.ship.module1 != null && this.hitTestObject(Game.main.ship.module1))
				{				
					// Remove health from ship
					Game.main.ship.module1.updateHealth(-25);
					
					// Delete missile instance
					onDelete();
					return;
				}
				if(Game.main.ship.module2 != null && this.hitTestObject(Game.main.ship.module2))
				{				
					// Remove health from ship
					Game.main.ship.module2.updateHealth(-25);
					
					// Delete missile instance
					onDelete();
					return;
				}
				
				// Delete missile instance once it's not in the frame anymore
				if(x < 0)
					onDelete();
			}
		}
		
		/***********************************************************
		* Method		: onDelete
		* Purpose		: called for deleting the object instance
		* Parameters	: None
		* return		: void
		***********************************************************/
		public function onDelete()
		{
			removeEventListener(Event.ENTER_FRAME, enterFrame);
			Game.main.spriteClip.removeChild(this);
		}
	}
}